using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using NDomain;
using System.Collections;

namespace NDomain.ExampleModels.BloggingSystem
{
    [Entity]
    [AggregateRoot]
    public class Blog
    {
        private long _id;

        public virtual long Id
        {
            get { return _id; }
        }

        private User _owner = User.NullInstance;

        public virtual User Owner
        {
            get { return _owner; }
        }

        private string _name;
        private IBlogRepository _blogRepository;

        public virtual string Name
        {
            get { return _name; }
            set 
            {
                AssertIsUnique(value);
                _name = value; 
            }
        }

        private void AssertIsUnique(string newName)
        {
            //TODO: This logic could (should?) be moved into the Repository
            if (newName != _name && _blogRepository.ExistsByName(newName))
                throw new InvalidOperationException("Blog name already in use");
        }

        private IList<Article> _articles = new List<Article>();

        public virtual IList<Article> Articles
        {
            get { return new List<Article>(_articles).AsReadOnly(); }
        }

        public virtual Article NewArticle(string title, string text, string author)
        {
            Article newArticle = new Article(title, text, author);
            _articles.Add(newArticle);
            return newArticle;
        }

        public Blog(string name, IBlogRepository blogRepository)
        {
            _blogRepository = blogRepository;
            Name = name;
        }

        protected Blog()
        {
            
        }
	
    }
}
